<?php

namespace App\Models\College;

use App\Models\BaseModel;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class CollegeTrainingPlanModel extends BaseModel
{
    use KfUid;
    protected $table = 'college_training_plans';

    protected $fillable = [
        'id',
        'name',
        'type',
        'campus',
        'planStartTime',
        'planEndTime',
        'courseStartTime',
        'teacherShopId',
        'studentShopId',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];

    const TYPE_CAMPUS = 1;
    const TYPE_DEPENDENCY = 2;

    const TYPE_MAPPING = [
        self::TYPE_CAMPUS => '校区培训',
        self::TYPE_DEPENDENCY => '属地培训',
    ];

    const TRAINING_PLANS_STATUS_DEFAULT = 0;
    const TRAINING_PLANS_STATUS_SUC = 1;
    const TRAINING_PLANS_STATUS_OFF = 2;

    const TRAINING_PLANS_STATUS_MAPPING = [
        self::TRAINING_PLANS_STATUS_DEFAULT => '正常',
        self::TRAINING_PLANS_STATUS_SUC => '完成',
        self::TRAINING_PLANS_STATUS_OFF => '关闭',
    ];

    const TRAINING_PLANS_CAMPUS_BJ = 1;//校区 1北京
    const TRAINING_PLANS_CAMPUS_SH = 2;//校区 2上海
    const TRAINING_PLANS_CAMPUS_SZ = 3;//校区 3深圳
    const TRAINING_PLANS_CAMPUS_OTHER = 4;//校区 4其他
    const TRAINING_PLANS_CAMPUS_WH = 5; // 校区 5武汉
    const TRAINING_PLANS_CAMPUS_XA = 6; // 校区 6西安

    const TRAINING_PLANS_CAMPUS_MAPPING = [
        self::TRAINING_PLANS_CAMPUS_BJ => '北京校区',
        self::TRAINING_PLANS_CAMPUS_SH => '上海校区',
        self::TRAINING_PLANS_CAMPUS_SZ => '深圳校区',
        self::TRAINING_PLANS_CAMPUS_WH => '武汉校区',
        self::TRAINING_PLANS_CAMPUS_XA => '西安校区',
        self::TRAINING_PLANS_CAMPUS_OTHER => '其他',
    ];

    //对应配置表枚举
    const CAMPUS_CONFIG_MAPPING = [
        self::TRAINING_PLANS_CAMPUS_BJ  => 'beijing',
        self::TRAINING_PLANS_CAMPUS_SH  => 'shanghai',
        self::TRAINING_PLANS_CAMPUS_SZ  => 'shenzhen',
        self::TRAINING_PLANS_CAMPUS_WH  => 'wuhan',
        self::TRAINING_PLANS_CAMPUS_XA  => 'xian',
    ];

    public function orderRel(): HasMany
    {
        return $this->hasMany(CollegeTrainingOrderModel::class, 'planId');
    }

    public function orderPlanUserRel(): HasMany
    {
        return $this->hasMany(CollegeTrainingOrderPlanUserModel::class, 'trainingOrderId');
    }

    public function orderUserRel(): HasMany
    {
        return $this->hasMany(CollegeTrainingOrderUserModel::class, 'trainingOrderId');
    }

    /**
     * 根据id查询培训计划详情
     * @param $id
     * @return Builder|Model|object|null
     */
    public static function getTrainingPlansInfoById($id)
    {
        return self::query()
            ->where(['id' => $id])
            ->select('id', 'name', 'type', 'campus', 'planStartTime', 'planEndTime', 'courseStartTime', 'createdAt', 'status')
            ->first();
    }

    /**
     * 新增培训计划
     * @param array $data
     * @return Model
     */
    public static function addTrainingPlans($data)
    {
        return self::query()->create($data);
    }


    /**
     * 修改培训计划
     * @param $id
     * @param $data
     * @return int
     */
    public static function setTrainingPlansInfoById($id, $data): int
    {
        return self::query()->where(['id' => $id])->update($data);
    }
}
